package com.zhongxi.guoyuan.view.seller;

import android.content.SharedPreferences;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.seller.SellerCoinOrderComplainAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.BaseResult;
import com.zhongxi.guoyuan.entity.seller.CoinCenterResult;
import com.zhongxi.guoyuan.entity.seller.ComplaintData;
import com.zhongxi.guoyuan.url.Url;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class SellerCoinOrderComplainActivity extends BaseActivity implements View.OnClickListener, PullToRefreshBase.OnRefreshListener2<ListView> {

    private PullToRefreshListView lvList;
    private List<ComplaintData> data = new ArrayList<>();
//    private TextView tvBack;
    private SellerCoinOrderComplainAdapter adapter;
    private String type;
    private TextView tvName;
    private TextView tvShopId;
    private TextView tvShopPhone;
    private TextView tvAllCoin;
    private TextView tvComplaintNum;
    private String complainNum;
    private RelativeLayout relaComplain;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_seller_coin_order_complain);
        initTitle();
        tvTitle.setText("银币商家中心");

        complainNum = getIntent().getStringExtra("complainNum");
        lvList = (PullToRefreshListView) findViewById(R.id.lvList);
//        tvBack = (TextView) findViewById(R.id.tvBack);
        relaComplain = (RelativeLayout) findViewById(R.id.relaComplain);
        tvName = (TextView) findViewById(R.id.tvName);
        tvShopId = (TextView) findViewById(R.id.tvShopId);
        tvShopPhone = (TextView) findViewById(R.id.tvShopPhone);
        tvAllCoin = (TextView) findViewById(R.id.tvAllCoin);
        tvComplaintNum = (TextView) findViewById(R.id.tvComplaintNum);
        tvComplaintNum.setText(complainNum);
    }

    private void initCoinData(CoinCenterResult result) {
        if(null!=result.getData().getName()&&!"".equals(result.getData().getName())){
            tvName.setText(result.getData().getName());
        }else{
            tvName.setText("");
        }
        if(null!=result.getData().getId()&&!"".equals(result.getData().getId())){
            tvShopId.setText("商家ID："+result.getData().getId());
        }else{
            tvShopId.setText("商家ID：****");
        }
        if(null!=result.getData().getPhone()&&!"".equals(result.getData().getPhone())){
            tvShopPhone.setText("商家电话："+result.getData().getPhone());
        }else{
            tvShopPhone.setText("商家电话：****");
        }
        if(null!=result.getEarningsNum()&&!"".equals(result.getEarningsNum())){
            tvAllCoin.setText(result.getEarningsNum());
        }else{
            tvAllCoin.setText("");
        }
        if(null!=result.getComplaintsNum()&&!"".equals(result.getComplaintsNum())){
            tvComplaintNum.setText(result.getComplaintsNum());
        }else{
            tvComplaintNum.setText("");
        }
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new SellerCoinOrderComplainAdapter(context, data);
        lvList.setAdapter(adapter);
        getInfo();
    }

    private void getInfo() {
        type = "info";
        getServer(Url.SILVER, null, "", true, null, "get");
    }

    private void getComplaint() {
        type = "complaint";
        String str = "&page="+pageNum;
        getServer(Url.SILVER_COMPLAINTS, null, str, true, null, "get");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            if("info".equals(type)){
                CoinCenterResult result = gson.fromJson(response, CoinCenterResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                initCoinData(result);
                getComplaint();
            }else if("complaint".equals(type)) {
                JSONObject obj = new JSONObject(response);
                JSONArray ary = obj.getJSONArray("data");
                JSONObject baseObj = obj.getJSONObject("safeobj");
                List<ComplaintData> complaintData = (List<ComplaintData>) ComplaintData.parseMatch(ary);
                BaseResult baseResult = (BaseResult) BaseResult.parseMatch(baseObj);
                MyApplication.getInstance().setSpValue(baseResult);
                if (pageNum == 1) {
                    data.clear();
                }
                if (complaintData.size() > 0) {
                    data.addAll(complaintData);
                }
                adapter.setJSONObject(obj);
                adapter.notifyDataSetChanged();
                lvList.onRefreshComplete();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        BaseResponse result = gson.fromJson(response, BaseResponse.class);
        if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
            MyApplication.getInstance().setSpValue(result.getSafeobj());
        }
        if(result.getCode() == 9002){
            showShortToast("登录已失效，请重新登录");
            SharedPreferences sharedPrefrences = getSharedPreferences(Url.APP_SP, MODE_PRIVATE);
            sharedPrefrences.edit().putString("coincredentials", "").commit();
            startAct(SellerCoinStoreLoginActivity.class);
        }else if(result.getCode() == 9004){

        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
//        tvBack.setOnClickListener(this);
        lvList.setOnRefreshListener(this);
        relaComplain.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
            case R.id.relaComplain:
                finish();
                break;
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum = 1;
        getComplaint();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum++;
        getComplaint();
    }
}
